<template>
  <!-- 搜索工作栏 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-160px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable class="!w-120px">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CMS_TASK_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button type="primary" @click="handleSiteAll()" v-hasPermi="['cms:content:staticpage']">
          生成本站静态页
        </el-button>
        <el-button
          type="primary"
          @click="handleUpdateIndex()"
          v-hasPermi="['cms:content:staticpage']"
        >
          生成首页
        </el-button>
        <el-button
          v-if="useUserStore().grade >= 2"
          type="primary"
          @click="handleIncludePage()"
          v-hasPermi="['cms:content:staticpage']"
        >
          生成包含页面
        </el-button>
        <el-button
          type="primary"
          @click="handleUpdateChannel()"
          v-hasPermi="['cms:content:staticpage']"
        >
          生成栏目页
        </el-button>
        <el-button
          type="primary"
          @click="handleUpdateContent()"
          v-hasPermi="['cms:content:staticpage']"
        >
          生成内容页
        </el-button>
        <el-button
          type="primary"
          @click="handleDeleteTasks()"
          v-hasPermi="['cms:content:staticpage']"
        >
          删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      ref="tableRef"
      row-key="id"
      @selection-change="handleDataSelectionChange"
    >
      <el-table-column type="selection" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column
        label="开始时间"
        align="center"
        prop="beginTime"
        width="180"
        :formatter="dateFormatter"
      />
      <el-table-column
        label="结束时间"
        align="center"
        prop="endTime"
        width="180"
        :formatter="dateFormatter"
      />
      <el-table-column label="完成数量" align="center" prop="currentNum" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CMS_TASK_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('detail', scope.row.id)">
            详情
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['cms:model:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <!-- 表单弹窗：详情 -->
  <TaskForm ref="formRef" @success="getList" />
</template>
<script setup lang="ts" name="Task">
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { TaskVO } from '@/api/cms/task/index'
import * as TaskApi from '@/api/cms/task'
import TaskForm from './TaskForm.vue'
import { useUserStore } from '@/store/modules/user'
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const tableRef = ref() // 表格的 Ref
const ids = ref<number[]>([]) // 表格的选中 ID 数组
/** 当内容主操作表格选择项发生变化时会触发该事件  */
const handleDataSelectionChange = (array: TaskVO[]) => {
  ids.value = []
  if (!array) {
    return
  }
  array.forEach((row) => ids.value.push(row.id))
}
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  taskType: 1,
  name: '',
  isGlobal: undefined,
  status: undefined
})
const queryFormRef = ref() // 搜索的表单
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await TaskApi.getTaskPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 查看详情 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await TaskApi.deleteTask(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
//批量删除任务
const handleDeleteTasks = async () => {
  let taskIds = ids.value
  if (taskIds.length === 0) {
    return
  }
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await TaskApi.deleteTasks(taskIds)
    message.success(t('common.delSuccess'))
    tableRef.value.clearSelection()
    // 刷新列表
    await getList()
  } catch {}
}
//生成本站首页静态页
const handleUpdateIndex = async () => {
  try {
    await TaskApi.staticpageIndex()
    message.success(t('common.success'))
    // 刷新列表
    await getList()
  } catch {}
}
//生成本站包含页面静态页
const handleIncludePage = async () => {
  try {
    await TaskApi.staticpageInclude()
    message.success(t('common.success'))
    // 刷新列表
    await getList()
  } catch {}
}
//生成本站全部静态页
const handleSiteAll = async () => {
  try {
    await TaskApi.staticpageAll()
    message.success(t('common.success'))
    // 刷新列表
    await getList()
  } catch {}
}
//生成本站栏目页面静态页
const handleUpdateChannel = async () => {
  try {
    await TaskApi.staticpageChannel()
    message.success(t('common.success'))
    // 刷新列表
    await getList()
  } catch {}
}
//生成本站内容页面静态页
const handleUpdateContent = async () => {
  try {
    await TaskApi.staticpageContent()
    message.success(t('common.success'))
    // 刷新列表
    await getList()
  } catch {}
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  queryParams.taskType = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
